/**
 * @class 中介者父类
 * @author DuskyHuang 圣子
 * @description 隔离视图逻辑与系统（V）
*/

import { _decorator, Component, director, Scene, } from 'cc';
import { notifier, mediators, bridge } from '../boot';
const { ccclass } = _decorator;

@ccclass('cfw.Mediator')
export default abstract class Mediator extends Component {

    protected __preload(): void {
        if (isFunc(super.__preload)) super.__preload();
        mediators.unshiftUnique(this);
    }

    protected on(type: any, handler: Handler, priority: number = 0): void {
        notifier.attach(type, handler, this, false, priority);
    }

    protected once(type: any, handler: Handler, priority: number = 0): void {
        notifier.attach(type, handler, this, true, priority);
    }

    protected off(type: any, handler: Handler): void {
        notifier.detach(type, handler, this);
    }

    protected notify(type: any, ...args: any[]): void {
        notifier.dispatch(type, ...args);
    }

    protected onDestroy(): void {
        notifier.unload(this);
        bridge.unload(this);
        mediators.remove(this);
    }

    /**
      * 当前场景
     */
    protected get scene(): Scene { return director.getScene(); }
}